package com.springboot.demo.controller;


import com.cx.item.common.exception.ParamsCheckException;
import com.cx.item.common.vo.ResultPageVo;
import com.springboot.demo.service.AttachmentFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 访问图片前缀需要添加 url/uploads/sun-miniapp/数据库保存图片路径
 * Created by hwm on 2018/6/5.
 */
@Api(description = "文件上传")
@Controller
@RequestMapping("/uploadFile")
public class UploadFileController {

    @Autowired
    private AttachmentFileService attachmentFileService;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "上传文件")
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public ResultPageVo<Map<String, String>> uploadFile(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception {

        if (file == null) {
            throw new ParamsCheckException("请选择上传文件");
        }

        Map<String, String> uploadFile = attachmentFileService.uploadFile(file);

        return ResultPageVo.success(uploadFile);
    }

}
